'''
Created on Jul 16, 2009

@author: xin
@author: Mikael Rousson
'''
import numpy
from scipy import ndimage

import pyr_band

def lpyram_extract_nbands(pyr, pind, scale, diml):
    """
    @param scale Actual scale 
    """
    
    pind = numpy.array(pind)
    ht = pind.shape[0]
    dime = []
    Z = []
    ct = 0
    if diml is 0 :
        diml = numpy.array([diml])
        
    for i in range(len(diml)):
        d = diml[i]
        lev = scale + d # current level to be extracted
        if ((lev > 0) and (lev < ht + 1)):
            
            tmp = pyr_band.pyr_band(pyr, pind, lev)
            
            while (d > 0):         
                tmp = ndimage.interpolation.zoom(tmp, 2) 
                
                if tmp.shape[0] > pind[scale, 0] :
                    tmp = tmp[0:tmp.shape[0], :]
                   
                if tmp.shape[1] > pind[scale, 1] :
                    tmp = tmp[:, 0:tmp.shape[1]]
                d = d - 1
                
            while(d < 0):    
                tmp = ndimage.interpolation.zoom(tmp, 0.5) 
                d = d + 1
                
          
            indices = tmp.shape
            Y = numpy.ndarray(shape=(len(diml), indices[0], indices[1]))
            Y[ct, :, :] = tmp
            Z.append(tmp)
            dime.append(i) 
            ct = ct + 1
    
    return Z, dime

